<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Dialogs in Java SWT</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Java, dialogs, SWT, programming, Linux">
<meta name="description" content="In this part of the Java SWT tutorial, we present
dialogs.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content2">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Dialogs</h1>


<p>
In this part of the Java SWT tutorial, we will introduce dialogs. 
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications.
A dialog is defined as a conversation between two or more persons. In a computer
application a dialog is a window which is used to "talk" to the application.
A dialog is used to input data, modify data, change the application settings etc.
Dialogs are important means of communication between a user and a computer program.
</p>


<h2>Directory dialog</h2>

<p>
The directory dialog is a dialog, which is used to select a path to a
certain directory.
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * This example shows a directory dialog
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {

    private Shell shell;

    public SWTApp(Display display) {

        shell = new Shell(display);

        shell.setText("DirectoryDialog");

        initUI();

        shell.setSize(350, 250);
        shell.setLocation(300, 300);

        shell.open();

        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
    }


    public void initUI() {


        final Label status = new Label(shell, SWT.BORDER);
        status.setText("Ready");
        FormLayout layout = new FormLayout();
        shell.setLayout(layout);

        FormData labelData = new FormData();
        labelData.left = new FormAttachment(0);
        labelData.right = new FormAttachment(100);
        labelData.bottom = new FormAttachment(100);
        status.setLayoutData(labelData);

        shell.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDown(MouseEvent event) {
                DirectoryDialog dialog = new DirectoryDialog(shell);
                String path = dialog.open();
                if (path != null)
                    status.setText(path);
            }

        });
    }


    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
In our example, we use the directory dialog to select a directory. The path to 
the directory is shown in the status label. The dialog is shown by 
clicking on the area of the window. 
</p>

<pre class="explanation">
DirectoryDialog dialog = new DirectoryDialog(shell);
</pre>

<p>
<code>DirectoryDialog</code> is created. 
</p>

<pre class="explanation">
String path = dialog.open();
</pre>

<p>
We get the path to the selected directory. 
</p>

<pre class="explanation">
if (path != null)
    status.setText(path);
</pre>

<p>
If the path is not null, we show the path in the status label.
</p>

<img src="/img/gui/swt/directorydialog.png" alt="Directory dialog">
<div class="figure">Figure: Directory dialog</div>


<h2>FontDialog</h2>

<p>
The <code>FontDialog</code> is a dialog for selecting fonts. 
It is typically used in applications, that do some text editing or formatting. 
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * This example shows a font dialog
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {

    private Shell shell;

    public SWTApp(Display display) {

        shell = new Shell(display);

        shell.setText("FontDialog");

        initUI();

        shell.setSize(350, 250);
        shell.setLocation(300, 300);

        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public void initUI() {


        final Label label = new Label(shell, SWT.NONE);
        label.setText("ZetCode Java SWT tutorial");

        label.setLocation(50, 50);
        label.pack();


        shell.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDown(MouseEvent event) {
                FontDialog dialog = new FontDialog(shell);
                FontData fdata = dialog.open();

                if (fdata != null) {

                    Font font = new Font(shell.getDisplay(), fdata);
                
                    label.setFont(font);
                    label.pack();
                    font.dispose();
                }                
            }
        });
    }

    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
In the code example, we use a <code>FontDialog</code> to change the
font of a label. 
</p>

<pre class="explanation">
FontDialog dialog = new FontDialog(shell);
</pre>

<p>
We create the <code>FontDialog</code>.
</p>

<pre class="explanation">
Font font = new Font(shell.getDisplay(), fdata);
</pre>

<p>
<code>Font</code> object is created from the font data, 
returned by the font dialog. 
</p>

<img src="/img/gui/swt/fontdialog.png" alt="FontDialog">
<div class="figure">Figure: FontDialog </div>


<h2>ColorDialog</h2>

<p>
<code>ColorDialog</code> is a dialog for selecting a color. 
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * This example shows a color dialog
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {

    private Shell shell;

    public SWTApp(Display display) {

        shell = new Shell(display);

        shell.setText("ColorDialog");

        initUI();

        shell.setSize(350, 250);
        shell.setLocation(300, 300);

        shell.open();

        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
    }


    public void initUI() {


        final Label label = new Label(shell, SWT.NONE);
        label.setText("ZetCode Java SWT tutorial");

        label.setLocation(50, 50);
        label.pack();


        shell.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDown(MouseEvent event) {
                ColorDialog dialog = new ColorDialog(shell);
                RGB rgb = dialog.open();
                if (rgb != null) {
                    Color col = new Color(shell.getDisplay(), rgb);
                    label.setForeground(col);
                    col.dispose();
                }
            }

        });
    }


    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
The example is very similar to the previous one. This time we 
change the color of the label. 
</p>

<pre class="explanation">
ColorDialog dialog = new ColorDialog(shell);
</pre>

<p>
We create the <code>ColorDialog</code>.
</p>

<pre class="explanation">
RGB rgb = dialog.open();
</pre>

<p>
We get the RGB value. 
</p>

<pre class="explanation">
Color col = new Color(shell.getDisplay(), rgb);
label.setForeground(col);
</pre>

<p>
We get the color value and modify the color of the label. 
</p>

<img src="/img/gui/swt/colordialog.png" alt="ColorDialog">
<div class="figure">Figure: ColorDialog </div>


<h2>FileDialog</h2>

<p>
The <code>FileDialog</code> is used to select a name of a file. 
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * This example shows a file dialog
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {

    private Shell shell;

    public SWTApp(Display display) {

        shell = new Shell(display);

        shell.setText("FileDialog");

        initUI();

        shell.setSize(350, 250);
        shell.setLocation(300, 300);

        shell.open();

        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
    }


    public void initUI() {


        final Label label = new Label(shell, SWT.NONE);
        label.setText("ZetCode Java SWT tutorial");

        label.setLocation(50, 50);
        label.pack();


        shell.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDown(MouseEvent event) {
                FileDialog dialog = new FileDialog(shell, SWT.OPEN);

                String[] filterNames = new String[] 
                    {"Java sources", "All Files (*)"};

                String[] filterExtensions = new String[] 
                    {"*.java", "*"};

                dialog.setFilterNames(filterNames);
                dialog.setFilterExtensions(filterExtensions);

                String path = dialog.open();
                if (path != null) {
                    label.setText(path);
                    label.pack();
                }
            }
        });
    }


    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
The code example uses a <code>FileDialog</code> to select a file.
The dialog uses a filter to show only the java sources. The name of the file
is shown in the label. 
</p>

<pre class="explanation">
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
</pre>

<p>
We create an <code>FileDialog</code> with a 
<code>SWT.OPEN</code> flag. The dialog can
be used to open or save files. 
</p>

<pre class="explanation">
String[] filterNames = new String[] 
{"Java sources", "All Files (*)"};

String[] filterExtensions = new String[] 
    {"*.java", "*"};

dialog.setFilterNames(filterNames);
dialog.setFilterExtensions(filterExtensions);
</pre>

<p>
We use a filter to narrow the files to Java sources.
</p>

<p>
This part of the Java SWT tutorial was about dialogs in SWT. 
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>

<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified June 14, 2009  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

